package com.xiaoyu.lanling.feature.launch.activity

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.emoji.widget.EmojiTextView
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.utils.MMKVUtil
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.activity.base.AppCompatToolbarActivity
import com.xiaoyu.lanling.app.PrivacySDKInitProcessor
import com.xiaoyu.lanling.common.Constant
import com.xiaoyu.lanling.router.Router
import com.xiaoyu.lanling.util.TextViewUtil
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import java.util.concurrent.TimeUnit

class LaunchActivity : AppCompatToolbarActivity() {

    private var mDisposableWaitingFinish: Disposable? = null

    override fun onCreateSafelyAfterAppFinishInit(savedInstanceState: Bundle?) {
        super.onCreateSafelyAfterAppFinishInit(savedInstanceState)
        setLightStatusBar()
        setContentView(R.layout.activity_launch)

        initData()
    }

    private fun initData() {
        val agree: Boolean = MMKVUtil.getDefault().getBoolean(Constant.MMKVKey.KEY_USER_AND_PRIVACY_PROTOCOL_AGREE, false)
        if (agree) {
            mDisposableWaitingFinish = Single.timer(200, TimeUnit.MILLISECONDS)
                    .observeOn(AndroidSchedulers.mainThread())
                    .filter { !isFinishing }
                    .subscribe { onFinish() }
        } else {
            tryShowPrivacyDialog()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        mDisposableWaitingFinish?.dispose()
    }


    private fun onFinish() {
        Router.instance.dispatchAfterLaunch(this)
        finish()
    }


    private fun tryShowPrivacyDialog() {
        if (isFinishing || isDestroyed) {
            onFinish()
            return
        }
        val contentView: View = LayoutInflater.from(this).inflate(R.layout.view_privacy_policy_dialog, null)
        val messageView: EmojiTextView = contentView.findViewById(R.id.dialog_content)
        TextViewUtil.stripUnderlinesAndClickable(messageView, "请认真阅读并理解<a style=\"font-weight: bold;\"; href=\"https://nuanchat.oss-cn-hangzhou.aliyuncs.com/h5/protocol/user-protocol.html?needLogin=false\"><font color='#777777'><b>《用户协议》</b></font></a>、<a style=\"font-weight: bold;\"; href=\"https://nuanchat.oss-cn-hangzhou.aliyuncs.com/h5/protocol/privacy-protocol.html?needLogin=false\"><b>《隐私协议》</b></a>，点击同意即代表你已阅读并同意全部条款。")
        val dialog = AlertDialog.Builder(this)
                .setTitle(getString(R.string.login_user_and_privacy_dialog_protocol_title))
                .setView(contentView)
                .setPositiveButton(getString(R.string.login_user_and_privacy_dialog_protocol_positive_button)) { _, _ ->
                    MMKVUtil.getDefault().putBoolean(Constant.MMKVKey.KEY_USER_AND_PRIVACY_PROTOCOL_AGREE, true)
                    onFinish()
                    PrivacySDKInitProcessor.initSDKIfAgreePrivacyAsync(this)
                }
                .setNegativeButton(getString(R.string.login_user_and_privacy_dialog_protocol_negative_button)) { dialog, _ ->
                    dialog.dismiss()
                    showPrivacySecondDialog()
                }
                .setCancelable(false)
                .show()
        dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(AppContext.getColor(R.color.colorThriceText))
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(AppContext.getColor(R.color.colorSecondary))
    }

    private fun showPrivacySecondDialog() {
        val contentView: View = LayoutInflater.from(this).inflate(R.layout.view_privacy_policy_dialog, null)
        val messageView: EmojiTextView = contentView.findViewById(R.id.dialog_content)
        TextViewUtil.stripUnderlinesAndClickable(messageView, "在您使用暖聊服务之前，请务必同意使用暖聊<a style=\"font-weight: bold;\"; href=\"https://nuanchat.oss-cn-hangzhou.aliyuncs.com/h5/protocol/user-protocol.html?needLogin=false\"><font color='#777777'><b>《用户协议》</b></font></a>和<a style=\"font-weight: bold;\"; href=\"https://nuanchat.oss-cn-hangzhou.aliyuncs.com/h5/protocol/privacy-protocol.html?needLogin=false\"><b>《隐私协议》</b></a>，感谢您的信任！若您仍不同意本隐私政策，很遗憾我们将无法为您提供服务，感谢您的理解。")
        val dialog = AlertDialog.Builder(this)
                .setTitle(getString(R.string.login_user_and_privacy_second_dialog_protocol_title))
                .setView(contentView)
                .setPositiveButton(getString(R.string.login_user_and_privacy_second_dialog_protocol_positive_button)) { dialog, _ ->
                    dialog.dismiss()
                    tryShowPrivacyDialog()
                }
                .setCancelable(false)
                .show()
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(AppContext.getColor(R.color.colorSecondary))
    }
}